<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('投资红线') }}</span>
    </div>

    <el-table v-loading="loading" :data="dealSpoInvestmentRedList" border tooltip-effect="darkTable" :span-method="objectSpanMethod">
      <el-table-column :label="$t('指标类型')" align="center" prop="categoriesType" :formatter="categoriesTypeFormat" />
      <el-table-column :label="$t('内容')" align="center" prop="content">
        <template slot-scope="scope">
          <span>{{ scope.row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('是否触发')" align="center" prop="triggerState" :formatter="triggerStateFormat" />
      <el-table-column :label="$t('备注')" align="center" prop="remark">
        <template slot-scope="scope">
          <span>{{ scope.row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:red:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目定增-投资红线对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealSpoInvestmentRedForm ref="dealSpoInvestmentRedForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listDealSpoInvestmentRed, delDealSpoInvestmentRed, exportDealSpoInvestmentRed } from '@/api/deal/spo/red/dealSpoInvestmentRed'
import dealSpoInvestmentRedForm from './dealSpoInvestmentRedForm.vue'
export default {
    components: {
        dealSpoInvestmentRedForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 项目定增-投资红线表格数据
          dealSpoInvestmentRedList: [],
          // 弹出层标题
          title: '项目定增-投资红线',
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              dealId: this.$route.query.id,
              categoriesType: undefined,
              content: undefined,
              triggerState: undefined,
              sort: undefined,
              pageNum: 1,
              pageSize: 10
          },
          categoriesTypeOptions: [],
          triggerStateOptions: [],
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      this.getDicts('dd_categories_type').then(response => {
        this.categoriesTypeOptions = response.data
      })
    this.getDicts('sys_yes_no').then(response => {
      this.triggerStateOptions = response.data
    })
      this.getList()
  },
  methods: {
    /** 查询项目定增-投资红线列表 */
    getList() {
        this.loading = true
        listDealSpoInvestmentRed(this.queryParams).then(response => {
            this.dealSpoInvestmentRedList = response.rows
            this.total = response.total
            this.loading = false
        }).catch(() => {
            this.loading = false
        })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            dealId: undefined,
            categoriesType: undefined,
            content: undefined,
            triggerState: undefined,
            remark: undefined,
            sort: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealSpoInvestmentRedForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealSpoInvestmentRedForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return delDealSpoInvestmentRed(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(() => {
            this.loading = false
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return exportDealSpoInvestmentRed(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(() => {
        })
    },
    // 尽调清单大类字典翻译
    categoriesTypeFormat(row) {
      return this.selectDictLabel(this.categoriesTypeOptions, row.categoriesType)
    },
    // 触发状态字典翻译
    triggerStateFormat(row) {
      return this.selectDictLabel(this.triggerStateOptions, row.triggerState)
    },
    rowspan(data) {
      // 二维数组，用于存放单元格合并规则
      const spanArr = []
      // 用于存储相同项的开始index
      let position = 0
      data.forEach((item, index) => {
        if (index === 0) {
          spanArr.push(1)
          position = 0
        } else {
          if (data[index].categoriesType === data[index - 1].categoriesType) {
            spanArr[position] += 1 // 有相同项
            spanArr.push(0) // 名称相同后往数组里面加一项0
          } else {
            spanArr.push(1) // 同列的前后两行单元格不相同
            position = index
          }
        }
      })
      return spanArr
    },
    objectSpanMethod({ rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        const spanArr = this.rowspan(this.dealSpoInvestmentRedList)
        for (let i = 0; i < spanArr.length; i++) {
          const _row = spanArr[rowIndex]
          return {
            rowspan: _row,
            colspan: 1
          }
        }
      }
    }
  }
}
</script>
